from rest_framework import serializers
from .models import School, College, Team, ResearchGroup


class SchoolSerializer(serializers.HyperlinkedModelSerializer):
    """
    SchoolInfo
    """
    class Meta:
        model = School
        fields = (
            'url',
            'school_id',
            'school_name',
            'school_code',
            'school_nature')
     #   depth = 1


class CollegeSerializer(serializers.HyperlinkedModelSerializer):
    """
    InstituteInfo
    """
    readable_college_principal = serializers.SerializerMethodField()
    readable_school_name = serializers.SerializerMethodField()

    class Meta:
        model = College
        fields = (
            'url',
            'college_id',
            'college_name',
            'college_num',
            'college_principal',
            'college_phone',
            'school_name',
            'readable_college_principal',
            'readable_school_name'
        )

    def get_readable_college_principal(self, obj):
        return obj.college_principal.username

    def get_readable_school_name(self, obj):
        return obj.school_name.school_name


class TeamSerializer(serializers.HyperlinkedModelSerializer):
    """
    TeamInfo
    """
    readable_team_principal = serializers.SerializerMethodField()
    readable_college_name = serializers.SerializerMethodField()

    class Meta:
        model = Team
        fields = (
            'url',
            'team_id',
            'team_name',
            'team_principal',
            'team_phone',
            'college_name',
            'readable_team_principal',
            'readable_college_name'
        )

    def get_readable_team_principal(self, obj):
        return obj.team_principal.username

    def get_readable_college_name(self, obj):
        return obj.college_name.college_name


class ResearchGroupSerializer(serializers.HyperlinkedModelSerializer):
    """
    ResearchGroupInfo
    """
    readable_research_group_principal = serializers.SerializerMethodField()
    readable_research_group_participant = serializers.SerializerMethodField()
    readable_team_name = serializers.SerializerMethodField()
    readable_subject_name = serializers.SerializerMethodField()

    class Meta:
        model = ResearchGroup
        fields = (
            'url',
            'research_group_id',
            'research_group_name',
            'research_group_principal',
            'research_group_participant',
            'team_name',
            'subject_name',
            'readable_research_group_principal',
            'readable_research_group_participant',
            'readable_team_name',
            'readable_subject_name'
        )
        # depth = 1

    def get_readable_research_group_principal(self, obj):
        return obj.research_group_principal.username

    # def get_readable_research_group_participant(self, obj):
    #     return obj.research_group_participant.username
    def get_readable_research_group_participant(self, obj):
        participants = []
        for participant in obj.research_group_participant.all():
            participants.append(participant.username)
        return participants

    def get_readable_team_name(self, obj):
        return obj.team_name.team_name

    def get_readable_subject_name(self, obj):
        return obj.subject_name.subject_name
